package com.onlinelibrary.service.impl;

import com.onlinelibrary.mapper.BookMapper;
import com.onlinelibrary.mapper.BookTypeMapper;
import com.onlinelibrary.pojo.Book;
import com.onlinelibrary.result.Result;
import com.onlinelibrary.service.BookService;
import com.onlinelibrary.util.MinioUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class BookServiceImpl01 implements BookService {

    private final BookMapper bookMapper;

    private final BookTypeMapper bookTypeMapper;
    //minio
    private final MinioUtil minioUtil;

    @Value("${bookUrl}")
    private String bookUrl;

    @Override
    public Result list() {
        List<Book> books = bookMapper.list();
        return new Result(10000, "查询成功", books);
    }

    @Override
    public Result getBook(Book book) {
        Book book1 = bookMapper.getBook(book);
        return new Result(10000, "查询成功", book1);
    }

    @Override
    public Result addBook(Book book) {
        Book book1 = bookMapper.getBook(book);
        if (book1!=null){
            return new Result(1, "该图书已存在");
        }
        //初始化封面，避免没有上传封面情况发生
        book.setCoverImage(bookUrl);
        Integer result = bookMapper.addBook(book);
        if (result > 0) {
            bookTypeMapper.upBookNum(book);
            return new Result(10000, "添加成功");
        } else {
            return new Result(1, "失败");
        }
    }

    @Override
    public Result updateCoverImage(MultipartFile file, String isbn) {
        if (isbn == null) {
            return new Result(1, "isbn为null");
        }
//        String coverImage = minioUtil.addBookCoverImage(file, isbn);
        String coverImage = minioUtil.addDataInBucket(file, isbn, "book");
        if (coverImage == null) {
            return new Result(2, "头像上传失败");
        }
        //创建一个临时User对象，传入新url和id
        Book book = new Book();
        book.setCoverImage(coverImage);
        book.setIsbn(isbn);
        System.out.println(book.getIsbn());
        //在数据库中更新头像url
        Integer result = bookMapper.updateBook(book);
        if (result > 0) {
            //将更新后的数据返回前端
            Book book1 = bookMapper.getBook(book);
            return new Result(10000, "success！", book1);
        } else {
            return new Result(1, "error");
        }

    }

    @Override
    public Result updateBook(Book book) {
        Integer result1 = bookMapper.updateBook(book);
        if (result1 > 0) {
            return new Result(10000, "修改成功");
        } else {
            return new Result(1, "修改失败");
        }
    }

    @Override
    public Result getTableData(Book book) {
        System.out.println(book);
        System.out.println("起始索引："+(book.getId()-1)*book.getStock());
        //wwc
        book.setId((book.getId()-1)*book.getStock());
        List<Book> books = bookMapper.getTableData(book);
        Integer bookCount = bookMapper.getBookCount(book);
        System.out.println(bookCount);
        System.out.println(books);
        System.out.println("返回的长度为："+books.size());
        return new Result(10000, String.valueOf(bookCount), books);
    }

    @Override
    public Result deleteBook(Book book) {
        System.out.println(book);
        Integer i = bookMapper.deleteBook(book);
        System.out.println(i);
        if (i > 0) {
            return new Result(10000, "删除成功");
        } else {
            return new Result(1, "删除失败");
        }
    }

    @Override
    public Result getBooks(Book book1) {
        List<Book> list = bookMapper.list();
        ArrayList<Integer> list1=new ArrayList<>();
        ArrayList<ArrayList<Book>> list2=new ArrayList<>();

        for (Book book : list) {
            Integer tid = book.getTid();
            if (!list1.contains(tid)) {
                ArrayList<Book> tempBook = new ArrayList<>();
                list2.add(tempBook);
                list1.add(tid);
            }
            list2.get(tid - 1).add(book);
        }
        return new Result(10000, "查询成功", list2);
    }

    @Override
    public Result getTopImg() {
        List<String> topImg = minioUtil.getTopImg();
        return new Result(10000, "查询成功", topImg);
    }

    @Override
    public Result getBooksByBookName(Book book) {
        System.out.println(book);
        List<Book> bookList=bookMapper.getBooksByBookName(book);
        System.out.println(bookList);
        return new Result(10000, "查询成功", bookList);
    }


}
